import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  ManyToMany,
  JoinTable,
  OneToMany,
  JoinColumn,
} from 'typeorm';
import { Category } from '../../categories/entities/category.entity';
import { Tag } from '../../tags/entities/tag.entity';
import { Favorite } from '../../favorites/entities/favorite.entity';
import { Review } from '../../reviews/entities/review.entity';

/**
 * 工具实体
 */
@Entity('tools')
export class Tool {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ length: 200 })
  name: string;

  @Column({ length: 200, unique: true })
  slug: string;

  @Column({ type: 'text', nullable: true })
  description: string;

  @Column({ length: 500 })
  url: string;

  @Column({ length: 500, nullable: true })
  logo: string;

  @Column({ default: true })
  isFree: boolean;

  @Column({ length: 100, nullable: true })
  pricing: string;

  @Column({ type: 'decimal', precision: 3, scale: 2, default: 0.0 })
  rating: number;

  @Column({ default: 0 })
  views: number;

  @Column({ default: 0 })
  upvotes: number;

  @Column({ default: false })
  isFeatured: boolean;

  @Column({ default: false })
  isSponsored: boolean;

  @Column({ default: false })
  isPremium: boolean;

  @Column({ length: 500, nullable: true })
  screenshot: string;

  // 新增字段：多张截图（JSON格式存储）
  @Column({ type: 'json', nullable: true })
  screenshots: string[];

  // 新增字段：使用场景说明
  @Column({ type: 'text', nullable: true })
  useCases: string;

  // 新增字段：核心特性（JSON格式存储）
  @Column({ type: 'json', nullable: true })
  features: string[];

  // 新增字段：优缺点
  @Column({ type: 'json', nullable: true })
  pros: string[];

  @Column({ type: 'json', nullable: true })
  cons: string[];

  // 新增字段：官方文档链接
  @Column({ length: 500, nullable: true })
  docsUrl: string;

  // 新增字段：视频介绍链接
  @Column({ length: 500, nullable: true })
  videoUrl: string;

  // 新增字段：GitHub仓库
  @Column({ length: 500, nullable: true })
  githubUrl: string;

  // 新增字段：推荐理由
  @Column({ type: 'text', nullable: true })
  recommendation: string;

  // 新增字段：相关链接（JSON格式）
  @Column({ type: 'json', nullable: true })
  relatedLinks: { title: string; url: string }[];

  @Column({ nullable: true })
  categoryId: number;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  // 关联关系
  @ManyToOne(() => Category, (category) => category.tools, {
    nullable: true,
    onDelete: 'SET NULL',
  })
  @JoinColumn({ name: 'categoryId' })
  category: Category;

  @ManyToMany(() => Tag, (tag) => tag.tools)
  @JoinTable({
    name: 'tool_tags',
    joinColumn: { name: 'toolId', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'tagId', referencedColumnName: 'id' },
  })
  tags: Tag[];

  @OneToMany(() => Favorite, (favorite) => favorite.tool)
  favorites: Favorite[];

  @OneToMany(() => Review, (review) => review.tool)
  reviews: Review[];
}







